package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"path"
	"strings"
	"time"
)

var (
	dirs = []string{
		"/Volumes/LINJING1_3_SCN/VIDEO_TS",
		"/Users/showurl/Downloads/灵镜传奇DVD/灵镜传奇DVD-02/VIDEO_TS",
		"/Users/showurl/Downloads/灵镜传奇DVD/灵镜传奇DVD-03/VIDEO_TS",
		"/Users/showurl/Downloads/灵镜传奇DVD/灵镜传奇DVD-04/VIDEO_TS",
		"/Users/showurl/Downloads/灵镜传奇DVD/灵镜传奇DVD-05/VIDEO_TS",
		"/Volumes/____16_18_SCN/VIDEO_TS",
		"/Volumes/____19_21_SCN/VIDEO_TS",
		"/Volumes/____22_24_SCN/VIDEO_TS",
		"/Volumes/____25_27_SCN/VIDEO_TS",
		"/Volumes/____28_30_SCN/VIDEO_TS",
	}
	dirs2 = []string{
		"/Volumes/4___01_03_SCN/VIDEO_TS",
		"/Volumes/SHUIYUE22_24_SCN/VIDEO_TS",
		"/Volumes/SHUIYUE25_27_SCN/VIDEO_TS",
		"/Volumes/SHUIYUE28_30_SCN/VIDEO_TS",
		"/Volumes/SHUIYUE7_9OK_SCN/VIDEO_TS",
		"/Volumes/SUIYUE31_32_SCN/VIDEO_TS",
		"/Volumes/UNDEFINED/VIDEO_TS",
		"/Volumes/UNDEFINED 1/VIDEO_TS",
		"/Volumes/____04_06_SCN/VIDEO_TS",
		"/Volumes/____10_12_SCN/VIDEO_TS",
		"/Volumes/____10_12_SCN 1/VIDEO_TS",
	}
)

func Command(c *exec.Cmd) error {
	log.Println("执行命令: ", strings.Join(c.Args, " "))
	stdout, err := c.StdoutPipe()
	if err != nil {
		return err
	}
	go func() {
		reader := bufio.NewReader(stdout)
		for {
			readString, err := reader.ReadString('\n')
			if err != nil || err == io.EOF {
				break
			}
			fmt.Print(readString)
		}
	}()
	return c.Run()
}

func ljcq() {
	//c := make(chan interface{})
	for _, d := range dirs {
		readDir, err := os.ReadDir(d)
		if err != nil {
			log.Println(err.Error())
			continue
		}
		for _, entry := range readDir {
			filename := entry.Name()
			if strings.HasSuffix(filename, ".VOB") {
				go func() {
					split := strings.Split(d, "/")
					var dName = "unknown"
					if split[1] == "Volumes" {
						dName = split[2]
					} else {
						dName = split[5]
					}
					outDir := path.Join(`/Users/showurl/Desktop/灵镜传奇`, dName)
					_ = os.MkdirAll(outDir, 0777)
					outFile := path.Join(outDir, filename+".mp4")
					cmd := exec.Command(
						"ffmpeg",
						"-i",
						path.Join(d, filename),
						"-ac",
						"2",
						"-ab",
						"128k",
						"-vcodec",
						"libx264",
						"-crf",
						"18",
						"-threads",
						"0",
						outFile,
					)
					err = Command(cmd)
					if err != nil {
						log.Fatalf("\n文件%s错误：%s\n", outFile, err.Error())
						panic(err)
					}
					// 完成
					log.Printf("\n文件%s输出完成\n", outFile)
				}()
				time.Sleep(time.Second*3)
				//break
			}
		}
		//break
	}
	//<-c
}

func sydt() {
	//c := make(chan interface{})
	for _, d := range dirs2 {
		readDir, err := os.ReadDir(d)
		if err != nil {
			log.Println(err.Error())
			continue
		}
		for _, entry := range readDir {
			filename := entry.Name()
			if strings.HasSuffix(filename, ".VOB") {
				go func() {
					split := strings.Split(d, "/")
					var dName = "unknown"
					if split[1] == "Volumes" {
						dName = split[2]
					} else {
						dName = split[5]
					}
					outDir := path.Join(`/Users/showurl/Desktop/水月洞天2`, dName)
					_ = os.MkdirAll(outDir, 0777)
					outFile := path.Join(outDir, filename+".mp4")
					cmd := exec.Command(
						"ffmpeg",
						"-i",
						path.Join(d, filename),
						"-ac",
						"2",
						"-ab",
						"128k",
						"-vcodec",
						"libx264",
						"-crf",
						"18",
						"-threads",
						"0",
						outFile,
					)
					err = Command(cmd)
					if err != nil {
						panic(err)
					}
					// 完成
					log.Printf("\n文件%s输出完成\n", outFile)
				}()
				time.Sleep(time.Second*3)
				//break
			}
		}
		//break
	}
	//<-c
}

func main() {
	_ = os.RemoveAll(`/Users/showurl/Desktop/水月洞天2`)
	_ = os.RemoveAll(`/Users/showurl/Desktop/灵镜传奇`)
	go ljcq()
	sydt()
	time.Sleep(time.Hour*24)
}